<?php
namespace app\common\controller;
use think\Controller;
use think\Session;
use think\Request;

class AdminCommon extends Controller
{
    public function _initialize()
    {
        $admin_id = Session::get('ADMIN_ID');
        if(empty($admin_id)){
            $this->redirect('Open/login');
        }
        $adminInfo = db('admin_user')->where(array('id'=>$admin_id))->find();
        $this->assign("adminInfo",$adminInfo);
        $this->menu();
    }
    public function menu(){
        $topid = Session::get('MENU_TOP_ID');
        $where['status'] = 1;
        $where['type'] = 1;
        $where['v_type'] = 1;
        $top = db('admin_menu')->where($where)->order("des desc")->select();
        if($topid){
            $file = "menu_".$topid;
            $menus = fopen("public/admin/json/".$file.".json","r");
        }else{
            $file = "menu_".$top[0]['id'];
            $menus = fopen("public/admin/json/".$file.".json","r");
            $topid = $top[0]['id'];
            Session::set('MENU_TOP_ID',$topid);
        }
        $this->assign("menu_top",$top);
        $this->assign("menu_topid",$topid);
        foreach($top as $p){
            if($p['id'] == $topid){
                $lmh = $p['m'];
                $lah = $p['a'];
                $lch = $p['c'];
                $hurl = url("/$lmh/$lah/$lch");
                $this->assign("home_url",$hurl);
            }
        }
        $menu = array();
        if(!$menus){
            $menus = fopen("public/admin/json/".$file.".json","w");
            $menu['left'] = array();
            $wherel['pid'] = $topid;
            $wherel['status'] = 1;
            $wherel['type'] = 2;
            $wherel['v_type'] = 1;
            $left = db('admin_menu')->where($wherel)->order("des desc")->select();
            foreach ($left as $l=>$lf){
                $lm = $lf['m'];
                $la = $lf['a'];
                $lc = $lf['c'];
                $lclass = '';
                $lurl = url("/$lm/$la/$lc");
                $licon = $lf['icon'];
                $request = Request::instance();
                if($lm == $request->module()){
                    $lclass = 'layui-this';
                }elseif($k == 0){
                    $lclass = 'layui-this';
                }
                $menu['left'][$l]['title'] = $lf['title'];
                $menu['left'][$l]['class'] = $lclass;
                $menu['left'][$l]['icon'] = $lf['icon'];
                $menu['left'][$l]['href'] = $lurl;
                $menu['left'][$l]['spread'] = false;
            }
            fwrite($menus, json_encode($menu['left']));
        }
        fclose($menus);
        /*$where['status'] = 1;
        $where['type'] = 1;
        $where['v_type'] = 1;
        $top = db('admin_menu')->where($where)->order("des desc")->select();
        $menu['top'] = '';
        $menu['left'] = '';
        foreach ($top as $k=>$t){
            $m = $t['m'];
            $a = $t['a'];
            $c = $t['c'];
            $class = '';
            $url = url("$a/$c");
            $icon = $t['icon'];
            $request = Request::instance();
            if($m == $request->module()){
                $class = 'layui-this';
            }elseif($k == 0){
                $class = 'layui-this';
            }
            $menu['top'] .= '<li class="layui-nav-item '. $class .'"><a href="javascript:;">'. $t['title'] .'</a></li>';
            $wherel['status'] = 1;
            $wherel['type'] = 2;
            $wherel['v_type'] = 1;
            $wherel['pid'] = $t['id'];
            $left = db('admin_menu')->where($wherel)->order("des desc")->select();
            $menu['left'] .= '<ul class="layui-nav layui-nav-tree left-menu" lay-filter="left-menu">';
            foreach ($left as $l=>$lf){
                $lm = $lf['m'];
                $la = $lf['a'];
                $lc = $lf['c'];
                $lclass = '';
                $lurl = url("$lm/$la/$lc");
                $licon = $lf['icon'];
                $request = Request::instance();
                if($lm == $request->module()){
                    $lclass = 'layui-this';
                }elseif($k == 0){
                    $lclass = 'layui-this';
                }
                $menu['left'] .= '<li class="layui-nav-item '. $lclass .'"><a  href="javascript:;" data-url="'. $lurl .'" data-id="'. $lf['id'] .'">'. $lf['title'] .'</a></li>';
            }
            $menu['left'] .= '</ul>';
        }
        return $menu;*/
    }
}